//    Copyright (C) 2013  <lugocreation@gmail.com>
//
//    This program is free software: you can redistribute it and/or modify
//    it under the terms of the GNU General Public License as published by
//    the Free Software Foundation, either version 3 of the License, or
//    (at your option) any later version.
//
//    This program is distributed in the hope that it will be useful,
//    but WITHOUT ANY WARRANTY; without even the implied warranty of
//    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//    GNU General Public License for more details.
//
//    You should have received a copy of the GNU General Public License
//    along with this program.  If not, see <http://www.gnu.org/licenses/>.
package org.jpgolay.me.piece;

import java.awt.event.*;
import java.beans.*;
import javax.swing.*;
import javax.swing.event.*;
import org.jpgolay.gui.fixed.BasicMenu;
import org.jpgolay.gui.fixed.TableViewer;
import org.jpgolay.gui.iconBar.*;
import org.jpgolay.me.agency.Agency;
import org.jpgolay.me.customer.CustomerPanelController;
import org.jpgolay.me.delivery.DeliveryController;
import org.jpgolay.me.group.GroupController;
import org.jpgolay.me.item.ItemPanelController;
import org.jpgolay.me.order.NewOrderController;
import org.jpgolay.me.order.OrderController;
import org.jpgolay.me.payment.PaymentController;
import org.jpgolay.me.persistence.ObjectCRUD;
import org.jpgolay.me.piece.list.PieceListController;
import org.jpgolay.me.reminder.ReminderWPController;
import org.jpgolay.me.salesperson.SalesPerson;
import org.jpgolay.me.statistic.StatisticsWPController;
import org.jpgolay.me.supplier.SupplierPanelController;
import org.jpgolay.me.util.BrowserControl;
import org.jpgolay.me.util.Parameter;

// Referenced classes of package org.jpgolay.me.piece:
//            PiecesGUI, PiecesPrint
public class PiecesController {

    private class MouseClickPieceList extends MouseAdapter {

        public void mouseClicked(MouseEvent e) {
            if (e.getClickCount() == 2) {
                actionOpenOrder(pieceListCtrl.getPieceType(), (String) pGui.getPiecesTableViewer().getTable().getValueAt(pGui.getPiecesTableViewer().getTable().getSelectedRow(), 0));
            }
        }

        MouseClickPieceList() {
        }
    }

    private class MouseClickPaymentList extends MouseAdapter {

        public void mouseClicked(MouseEvent e) {
            if (e.getClickCount() == 2) {
                actionOpenOrder(paymentCtrl.getPieceType(), (String) pGui.getPaymentListTableViewer().getTable().getValueAt(pGui.getPaymentListTableViewer().getTable().getSelectedRow(), 0));
            }
        }

        MouseClickPaymentList() {
        }
    }

    private class MouseClickDeliveryList extends MouseAdapter {

        public void mouseClicked(MouseEvent e) {
            if (e.getClickCount() == 2) {
                actionOpenOrder(2, (String) pGui.getDeliveryTableViewer().getTable().getValueAt(pGui.getDeliveryTableViewer().getTable().getSelectedRow(), 0));
            }
        }

        MouseClickDeliveryList() {
        }
    }

    private class MouseClickCustomerList extends MouseAdapter {

        public void mouseClicked(MouseEvent e) {
            if (e.getClickCount() == 2) {
                OrderController oCtrl = (OrderController) pGui.getSelectedPiece();
                if (oCtrl != null) {
                    oCtrl.setCustomer((String) pGui.getCustomerTableViewer().getTable().getValueAt(pGui.getCustomerTableViewer().getTable().getSelectedRow(), 0));
                }
            }
        }

        MouseClickCustomerList() {
        }
    }

    private class MouseClickSupplierList extends MouseAdapter {

        public void mouseClicked(MouseEvent e) {
            if (e.getClickCount() == 2) {
                OrderController oCtrl = (OrderController) pGui.getSelectedPiece();
                if (oCtrl != null) {
                    oCtrl.setSupplier((String) pGui.getSupplierTableViewer().getTable().getValueAt(pGui.getSupplierTableViewer().getTable().getSelectedRow(), 0));
                }
            }
        }

        MouseClickSupplierList() {
        }
    }

    private class MouseClickItemList extends MouseAdapter {

        public void mouseClicked(MouseEvent e) {
            if (e.getClickCount() == 2) {
                OrderController oCtrl = (OrderController) pGui.getSelectedPiece();
                if (oCtrl != null) {
                    oCtrl.setItem((String) pGui.getItemTableViewer().getTable().getValueAt(pGui.getItemTableViewer().getTable().getSelectedRow(), 0));
                }
            }
        }

        MouseClickItemList() {
        }
    }

    public PiecesController(Parameter param) {
        msgTab = "";
        key = 0;
        write = false;
        desktop = param.getDesktop();
        this.param = param;
        crud = new ObjectCRUD();
        initLaw();
        initAll();
    }

    private void initLaw() {
        if (param.getAccessLaw().length() >= 29 && param.getAccessLaw().charAt(29) == '1') {
            write = true;
        }
    }

    private boolean initAll() {
        if (pGui == null || pGui.isClosed()) {
            pGui = new PiecesGUI();
            pGui.setDefaultCloseOperation(0);
            pGui.setIconifiable(true);
            pGui.setResizable(false);
            pGui.setMaximizable(false);
            pGui.setClosable(true);
            pGui.setVisible(true);
            pGui.toFront();
            pGui.addInternalFrameListener(new InternalFrameAdapter() {
                public void internalFrameClosing(InternalFrameEvent evt) {
                    actionExit();
                }
            });
            pGui.getWelcomeCustomerButton().addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent evt) {
                    initCustomer();
                }
            });
            pGui.getWelcomeDeliveryButton().addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent evt) {
                    initDelivery();
                }
            });
            pGui.getWelcomeGroupButton().addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent evt) {
                    initGroup();
                }
            });
            pGui.getWelcomeItemButton().addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent evt) {
                    initItem();
                }
            });
            pGui.getWelcomePaymentButton().addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent evt) {
                    initPayment();
                }
            });
            pGui.getWelcomePiecesButton().addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent evt) {
                    initPiecesList();
                }
            });
            pGui.getWelcomeSupplierButton().addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent evt) {
                    initSupplier();
                }
            });
            pGui.getPiecesCustomerButton().addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent evt) {
                    initCustomer();
                }
            });
            pGui.getPiecesDeliveryButton().addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent evt) {
                    initDelivery();
                }
            });
            pGui.getPiecesGroupButton().addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent evt) {
                    initGroup();
                }
            });
            pGui.getPiecesItemButton().addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent evt) {
                    initItem();
                }
            });
            pGui.getPiecesPaymentButton().addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent evt) {
                    initPayment();
                }
            });
            pGui.getPiecesPiecesButton().addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent evt) {
                    initPiecesList();
                }
            });
            pGui.getPiecesSupplierButton().addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent evt) {
                    initSupplier();
                }
            });
            pGui.getDeliveryCustomerButton().addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent evt) {
                    initCustomer();
                }
            });
            pGui.getDeliveryDeliveryButton().addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent evt) {
                    initDelivery();
                }
            });
            pGui.getDeliveryGroupButton().addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent evt) {
                    initGroup();
                }
            });
            pGui.getDeliveryItemButton().addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent evt) {
                    initItem();
                }
            });
            pGui.getDeliveryPaymentButton().addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent evt) {
                    initPayment();
                }
            });
            pGui.getDeliveryPiecesButton().addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent evt) {
                    initPiecesList();
                }
            });
            pGui.getDeliverySupplierButton().addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent evt) {
                    initSupplier();
                }
            });
            pGui.getGroupCustomerButton().addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent evt) {
                    initCustomer();
                }
            });
            pGui.getGroupDeliveryButton().addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent evt) {
                    initDelivery();
                }
            });
            pGui.getGroupGroupButton().addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent evt) {
                    initGroup();
                }
            });
            pGui.getGroupItemButton().addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent evt) {
                    initItem();
                }
            });
            pGui.getGroupPaymentButton().addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent evt) {
                    initPayment();
                }
            });
            pGui.getGroupPiecesButton().addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent evt) {
                    initPiecesList();
                }
            });
            pGui.getGroupSupplierButton().addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent evt) {
                    initSupplier();
                }
            });
            pGui.getPaymentCustomerButton().addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent evt) {
                    initCustomer();
                }
            });
            pGui.getPaymentDeliveryButton().addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent evt) {
                    initDelivery();
                }
            });
            pGui.getPaymentGroupButton().addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent evt) {
                    initGroup();
                }
            });
            pGui.getPaymentItemButton().addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent evt) {
                    initItem();
                }
            });
            pGui.getPaymentPaymentButton().addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent evt) {
                    initPayment();
                }
            });
            pGui.getPaymentPieceButton().addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent evt) {
                    initPiecesList();
                }
            });
            pGui.getPaymentSupplierButton().addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent evt) {
                    initSupplier();
                }
            });
            pGui.getCustomerCustomerButton().addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent evt) {
                    initCustomer();
                }
            });
            pGui.getCustomerDeliveryButton().addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent evt) {
                    initDelivery();
                }
            });
            pGui.getCustomerGroupButton().addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent evt) {
                    initGroup();
                }
            });
            pGui.getCustomerItemButton().addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent evt) {
                    initItem();
                }
            });
            pGui.getCustomerPaymentButton().addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent evt) {
                    initPayment();
                }
            });
            pGui.getCustomerPiecesButton().addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent evt) {
                    initPiecesList();
                }
            });
            pGui.getCustomerSupplierButton().addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent evt) {
                    initSupplier();
                }
            });
            pGui.getSupplierCustomerButton().addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent evt) {
                    initCustomer();
                }
            });
            pGui.getSupplierDeliveryButton().addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent evt) {
                    initDelivery();
                }
            });
            pGui.getSupplierGroupButton().addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent evt) {
                    initGroup();
                }
            });
            pGui.getSupplierItemButton().addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent evt) {
                    initItem();
                }
            });
            pGui.getSupplierPaymentButton().addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent evt) {
                    initPayment();
                }
            });
            pGui.getSupplierPiecesButton().addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent evt) {
                    initPiecesList();
                }
            });
            pGui.getSupplierSupplierButton().addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent evt) {
                    initSupplier();
                }
            });
            pGui.getItemCustomerButton().addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent evt) {
                    initCustomer();
                }
            });
            pGui.getItemDeliveryButton().addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent evt) {
                    initDelivery();
                }
            });
            pGui.getItemGroupButton().addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent evt) {
                    initGroup();
                }
            });
            pGui.getItemItemButton().addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent evt) {
                    initItem();
                }
            });
            pGui.getItemPaymentButton().addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent evt) {
                    initPayment();
                }
            });
            pGui.getItemPiecesButton().addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent evt) {
                    initPiecesList();
                }
            });
            pGui.getItemSupplierButton().addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent evt) {
                    initSupplier();
                }
            });
            pGui.getPiecesTabbedPane().addChangeListener(new ChangeListener() {
                public void stateChanged(ChangeEvent evt) {
                    actionRefreshMenuBarAndToolBar();
                }
            });
            initMenu();
            initIconBar();
            initPieces();
            initWelcome();
            initIcon();
            return true;
        }
        if (pGui.isIcon()) {
            try {
                pGui.setIcon(false);
            } catch (PropertyVetoException e) {
                e.printStackTrace();
            }
        }
        pGui.toFront();
        return false;
    }

    private void initMenu() {
        pGui.getMenu().getExit().addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent evt) {
                actionExit();
            }
        });
        pGui.getMenu().getDelete().addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent evt) {
                actionDelete();
            }
        });
        pGui.getMenu().getHelp().addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent evt) {
                actionHelp();
            }
        });
        pGui.getMenu().getModify().addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent evt) {
                actionModify();
            }
        });
        pGui.getMenu().getNew().addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent evt) {
                actionNew();
            }
        });
        pGui.getMenu().getOpen().addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent evt) {
                actionOpen();
            }
        });
        pGui.getMenu().getPrint().addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent evt) {
                actionPrint(true);
            }
        });
        pGui.getMenu().getSave().addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent evt) {
                actionSave();
            }
        });
        pGui.getMenu().getSave().setEnabled(false);
        pGui.getMenu().getModify().setEnabled(false);
        pGui.getMenu().getDelete().setEnabled(false);
        pGui.getMenu().getPrint().setEnabled(false);
        pGui.getMenu().getNew().setEnabled(write);
    }

    private void initIconBar() {
        pGui.getIconBar().addAnswerListener(new AnswerListener() {
            public void create(AnswerEvent e) {
                actionNew();
            }

            public void open(AnswerEvent e) {
                actionOpen();
            }

            public void modify(AnswerEvent e) {
                actionModify();
            }

            public void save(AnswerEvent e) {
                actionSave();
            }

            public void remove(AnswerEvent e) {
                actionDelete();
            }

            public void print(AnswerEvent e) {
                actionPrint(false);
            }

            public void help(AnswerEvent e) {
                actionHelp();
            }
        });
        pGui.getIconBar().setEnabledSave(false);
        pGui.getIconBar().setEnabledModify(false);
        pGui.getIconBar().setEnabledRemove(false);
        pGui.getIconBar().setEnabledPrint(false);
        pGui.getIconBar().setEnabledCreate(write);
    }

    private void initIcon() {
        quoteSales = new ImageIcon(getClass().getResource("/org/jpgolay/me/images/quoteS16x16.png"));
        orderSales = new ImageIcon(getClass().getResource("/org/jpgolay/me/images/orderS16x16.png"));
        deliverySales = new ImageIcon(getClass().getResource("/org/jpgolay/me/images/deliveryS16x16.png"));
        billSales = new ImageIcon(getClass().getResource("/org/jpgolay/me/images/billS16x16.png"));
        assetSales = new ImageIcon(getClass().getResource("/org/jpgolay/me/images/assetS16x16.png"));
        orderBuys = new ImageIcon(getClass().getResource("/org/jpgolay/me/images/orderB16x16.png"));
        billBuys = new ImageIcon(getClass().getResource("/org/jpgolay/me/images/billB16x16.png"));
        quoteSalesM = new ImageIcon(getClass().getResource("/org/jpgolay/me/images/mQuoteS16x16.png"));
        orderSalesM = new ImageIcon(getClass().getResource("/org/jpgolay/me/images/mOrderS16x16.png"));
        deliverySalesM = new ImageIcon(getClass().getResource("/org/jpgolay/me/images/mDeliveryS16x16.png"));
        billSalesM = new ImageIcon(getClass().getResource("/org/jpgolay/me/images/mBillS16x16.png"));
        assetSalesM = new ImageIcon(getClass().getResource("/org/jpgolay/me/images/mAssetS16x16.png"));
        orderBuysM = new ImageIcon(getClass().getResource("/org/jpgolay/me/images/mOrderB16x16.png"));
        billBuysM = new ImageIcon(getClass().getResource("/org/jpgolay/me/images/mBillB16x16.png"));
    }

    private void initPieces() {
    }

    private void initWelcome() {
        if (statWPCtrl == null) {
            statWPCtrl = new StatisticsWPController(pGui.getStatisticsWPGUI(), param, desktop);
        }
        if (reminderWPCtrl == null) {
            reminderWPCtrl = new ReminderWPController(pGui.getReminderWPGUIPanel(), param, desktop);
        }
    }

    private void initPiecesList() {
        if (pieceListCtrl == null) {
            pieceListCtrl = new PieceListController(pGui.getPiecesSearchBox(), pGui.getPiecesTableViewer(), desktop);
            pGui.getPiecesTableViewer().getTable().addMouseListener(new MouseClickPieceList());
        }
    }

    private void initDelivery() {
        if (deliveryCtrl == null) {
            deliveryCtrl = new DeliveryController(pGui.getDeliverySearchBox(), pGui.getDeliveryTableViewer(), param);
            pGui.getDeliveryTableViewer().getTable().addMouseListener(new MouseClickDeliveryList());
        }
    }

    private void initGroup() {
        if (groupCtrl == null) {
            groupCtrl = new GroupController(pGui.getGroupSearchBox(), pGui.getGroupTableViewer(), pGui.getGroupExecutPanel(), param, desktop);
        }
    }

    private void initPayment() {
        if (paymentCtrl == null) {
            paymentCtrl = new PaymentController(pGui.getPaymentSearchBox(), pGui.getPaymentListTableViewer(), pGui.getPaymentTableViewer(), pGui.getReminderTableViewer(), param, desktop);
            pGui.getPaymentListTableViewer().getTable().addMouseListener(new MouseClickPaymentList());
        }
    }

    private void initCustomer() {
        if (customerCtrl == null) {
            customerCtrl = new CustomerPanelController(pGui.getCustomerSearchBox(), pGui.getCustomerTableViewer(), desktop);
            pGui.getCustomerTableViewer().getTable().addMouseListener(new MouseClickCustomerList());
        }
    }

    private void initSupplier() {
        if (supplierCtrl == null) {
            supplierCtrl = new SupplierPanelController(pGui.getSupplierSearchBox(), pGui.getSupplierTableViewer(), desktop);
            pGui.getSupplierTableViewer().getTable().addMouseListener(new MouseClickSupplierList());
        }
    }

    private void initItem() {
        if (itemCtrl == null) {
            itemCtrl = new ItemPanelController(pGui.getItemSearchBox(), pGui.getItemTableViewer(), desktop);
            pGui.getItemTableViewer().getTable().addMouseListener(new MouseClickItemList());
            itemCtrl.addPropertyChangeListener(new PropertyChangeListener() {
                public void propertyChange(PropertyChangeEvent e) {
                    OrderController oCtrl = (OrderController) pGui.getSelectedPiece();
                    if (oCtrl != null) {
                        oCtrl.setItem((String) pGui.getItemTableViewer().getTable().getValueAt(0, 0));
                    }
                }
            });
        }
    }

    private void initNewOrder() {
        if (newOrderCtrl == null) {
            newOrderCtrl = new NewOrderController(desktop);
            newOrderCtrl.getOK().addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent evt) {
                    newOrderCtrl.actionOK();
                    actionNewOrder(newOrderCtrl.getTypePiece(), newOrderCtrl.getAgency(), newOrderCtrl.getSalesPerson());
                }
            });
            newOrderCtrl.getCANCEL().addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent evt) {
                    newOrderCtrl.actionCANCEL();
                }
            });
        } else {
            newOrderCtrl.init();
        }
    }

    private void actionNew() {
        initNewOrder();
    }

    private void actionOpen() {
        initPiecesList();
        pGui.openPiecesPanel();
    }

    private void actionSave() {
        OrderController oCtrl = (OrderController) pGui.getSelectedPiece();
        switch (oCtrl.getPieceType()) {
            case 0: // '\0'
                pieceIcon = quoteSales;
                break;

            case 1: // '\001'
                pieceIcon = orderSales;
                break;

            case 2: // '\002'
                pieceIcon = deliverySales;
                break;

            case 3: // '\003'
                pieceIcon = billSales;
                break;

            case 4: // '\004'
                pieceIcon = assetSales;
                break;

            case 5: // '\005'
                pieceIcon = orderBuys;
                break;

            case 6: // '\006'
                pieceIcon = billBuys;
                break;

            default:
                pieceIcon = quoteSales;
                break;
        }
        pGui.setIconPiece(pieceIcon);
        oCtrl.setEditable(false);
        pGui.getIconBar().setEnabledRemove(false);
        pGui.getIconBar().setEnabledSave(false);
        pGui.getIconBar().setEnabledModify(true);
        pGui.getMenu().getDelete().setEnabled(false);
        pGui.getMenu().getSave().setEnabled(false);
        pGui.getMenu().getModify().setEnabled(true);
    }

    private void actionPrint(boolean viewer) {
        OrderController oCtrl = (OrderController) pGui.getSelectedPiece();
        if (oCtrl.getPieceId() != null) {
            print = new PiecesPrint(oCtrl.getAgency(), oCtrl.getAddress(), oCtrl.getPiece(), oCtrl.getSalesPerson(), oCtrl.getRowsList(), viewer, param);
        }
    }

    private void actionExit() {
        groupCtrl = null;
        paymentCtrl = null;
        customerCtrl = null;
        itemCtrl = null;
        supplierCtrl = null;
        pieceListCtrl = null;
        deliveryCtrl = null;
        statWPCtrl = null;
        reminderWPCtrl = null;
        pGui.setVisible(false);
        pGui = null;
    }

    private void actionModify() {
        OrderController oCtrl = (OrderController) pGui.getSelectedPiece();
        switch (oCtrl.getPieceType()) {
            case 0: // '\0'
                pieceIcon = quoteSalesM;
                break;

            case 1: // '\001'
                pieceIcon = orderSalesM;
                break;

            case 2: // '\002'
                pieceIcon = deliverySalesM;
                break;

            case 3: // '\003'
                pieceIcon = billSalesM;
                break;

            case 4: // '\004'
                pieceIcon = assetSalesM;
                break;

            case 5: // '\005'
                pieceIcon = orderBuysM;
                break;

            case 6: // '\006'
                pieceIcon = billBuysM;
                break;

            default:
                pieceIcon = quoteSalesM;
                break;
        }
        pGui.setIconPiece(pieceIcon);
        oCtrl.setEditable(true);
        pGui.getIconBar().setEnabledRemove(true);
        pGui.getIconBar().setEnabledSave(true);
        pGui.getIconBar().setEnabledModify(false);
        pGui.getMenu().getDelete().setEnabled(true);
        pGui.getMenu().getSave().setEnabled(true);
        pGui.getMenu().getModify().setEnabled(false);
    }

    private void actionDelete() {
        OrderController oCtrl = (OrderController) pGui.getSelectedPiece();
        int answer = JOptionPane.showConfirmDialog(null, "Souhaitez vous vraiment supprimer la pi\350ce : " + oCtrl.getPieceId() + " ?", "Confirmation", 1, 2, null);
        switch (answer) {
            case -1:
            case 1: // '\001'
            case 2: // '\002'
            default:
                break;

            case 0: // '\0'
                if (actionRemovePiece(oCtrl)) {
                    pGui.removePiece();
                    pGui.getIconBar().setEnabledRemove(false);
                    pGui.getIconBar().setEnabledSave(false);
                    pGui.getIconBar().setEnabledModify(false);
                    pGui.getMenu().getDelete().setEnabled(false);
                    pGui.getMenu().getSave().setEnabled(false);
                    pGui.getMenu().getModify().setEnabled(false);
                } else {
                    JOptionPane.showMessageDialog(null, "Impossible de supprimer la piece " + oCtrl.getPieceId() + " !" + "\n(un autre utilisateur l'a peut-\352tre modifi\351e entre temps)");
                }
                break;
        }
    }

    private boolean actionRemovePiece(OrderController oCtrl) {
        switch (oCtrl.getPieceType()) {
            case 0: // '\0'
                return crud.delete(oCtrl.getPieceId(), oCtrl.getLastUpdatePiece(), 22);

            case 1: // '\001'
                return crud.delete(oCtrl.getPieceId(), oCtrl.getLastUpdatePiece(), 22);

            case 2: // '\002'
                return crud.delete(oCtrl.getPieceId(), oCtrl.getLastUpdatePiece(), 22);

            case 3: // '\003'
                return crud.delete(oCtrl.getPieceId(), oCtrl.getLastUpdatePiece(), 25);

            case 4: // '\004'
                return crud.delete(oCtrl.getPieceId(), oCtrl.getLastUpdatePiece(), 25);

            case 5: // '\005'
                return crud.delete(oCtrl.getPieceId(), oCtrl.getLastUpdatePiece(), 24);

            case 6: // '\006'
                return crud.delete(oCtrl.getPieceId(), oCtrl.getLastUpdatePiece(), 24);
        }
        return false;
    }

    private void actionHelp() {
        BrowserControl.displayURL("www.media-4-u.net");
    }

    private void actionRefreshMenuBarAndToolBar() {
        OrderController oCtrl = (OrderController) pGui.getSelectedPiece();
        if (oCtrl == null) {
            pGui.getIconBar().setEnabledRemove(false);
            pGui.getIconBar().setEnabledSave(false);
            pGui.getIconBar().setEnabledModify(false);
            pGui.getMenu().getDelete().setEnabled(false);
            pGui.getMenu().getSave().setEnabled(false);
            pGui.getMenu().getModify().setEnabled(false);
            pGui.getMenu().getPrint().setEnabled(false);
            pGui.getIconBar().setEnabledPrint(false);
            pGui.getMenu().getNew().setEnabled(write);
            pGui.getIconBar().setEnabledCreate(write);
        } else if (oCtrl.isEditable()) {
            pGui.getIconBar().setEnabledRemove(true);
            pGui.getIconBar().setEnabledSave(true);
            pGui.getIconBar().setEnabledModify(false);
            pGui.getMenu().getDelete().setEnabled(true);
            pGui.getMenu().getSave().setEnabled(true);
            pGui.getMenu().getModify().setEnabled(false);
        } else {
            pGui.getIconBar().setEnabledRemove(false);
            pGui.getIconBar().setEnabledSave(false);
            pGui.getMenu().getDelete().setEnabled(false);
            pGui.getMenu().getSave().setEnabled(false);
            if (oCtrl.isLocked()) {
                pGui.getIconBar().setEnabledModify(false);
                pGui.getMenu().getModify().setEnabled(false);
            } else {
                pGui.getIconBar().setEnabledModify(write);
                pGui.getMenu().getModify().setEnabled(write);
            }
        }
    }

    private void actionNewOrder(int pieceType, Agency agency, SalesPerson salesPerson) {
        OrderController oCtrl = new OrderController(getNewPiece(pieceType), agency, salesPerson, pieceType, param);
        pGui.addPiece(pieceId, pieceIcon, oCtrl, msgTab);
        pGui.getIconBar().setEnabledRemove(true);
        pGui.getIconBar().setEnabledSave(true);
        pGui.getIconBar().setEnabledModify(false);
        pGui.getMenu().getDelete().setEnabled(true);
        pGui.getMenu().getSave().setEnabled(true);
        pGui.getMenu().getModify().setEnabled(false);
        pGui.getMenu().getPrint().setEnabled(true);
        pGui.getIconBar().setEnabledPrint(true);
    }

    private Object getNewPiece(int type) {
        switch (type) {
            case 0: // '\0'
                msgTab = "DEVIS client";
                pieceIcon = quoteSalesM;
                pieceId = (String) crud.create(22, "D");
                return crud.read(pieceId, 22);

            case 1: // '\001'
                msgTab = "COMMANDE client";
                pieceIcon = orderSalesM;
                pieceId = (String) crud.create(22, "C");
                return crud.read(pieceId, 22);

            case 2: // '\002'
                msgTab = "BL client";
                pieceIcon = deliverySalesM;
                pieceId = (String) crud.create(22, "BL");
                return crud.read(pieceId, 22);

            case 3: // '\003'
                msgTab = "FACTURE client";
                pieceIcon = billSalesM;
                pieceId = (String) crud.create(25, "F");
                return crud.read(pieceId, 25);

            case 4: // '\004'
                msgTab = "AVOIR client";
                pieceIcon = assetSalesM;
                pieceId = (String) crud.create(25, "A");
                return crud.read(pieceId, 25);

            case 5: // '\005'
                msgTab = "COMMANDE fournisseur";
                pieceIcon = orderBuysM;
                pieceId = (String) crud.create(24, "C");
                return crud.read(pieceId, 24);

            case 6: // '\006'
                msgTab = "FACTURE fournisseur";
                pieceIcon = billBuysM;
                pieceId = (String) crud.create(24, "F");
                return crud.read(pieceId, 24);
        }
        return null;
    }

    private void actionOpenOrder(int pieceType, String pieceId) {
        OrderController oCtrl = null;
        boolean newPiece = true;
        int countPiece = pGui.getPiecesCount();
        if (countPiece != 0) {
            for (int i = 0; i < countPiece; i++) {
                if (pGui.getTitlePiece(i).equals(pieceId)) {
                    getOpenPiece(pieceType, pieceId);
                    pGui.setSelectedPiece(i, pieceIcon);
                    oCtrl = (OrderController) pGui.getSelectedPiece();
                    newPiece = false;
                }
            }

        }
        if (newPiece) {
            oCtrl = new OrderController(getOpenPiece(pieceType, pieceId), pieceType, param);
            pGui.addPiece(pieceId, pieceIcon, oCtrl, msgTab);
        }
        pGui.getIconBar().setEnabledRemove(false);
        pGui.getIconBar().setEnabledSave(false);
        pGui.getMenu().getDelete().setEnabled(false);
        pGui.getMenu().getSave().setEnabled(false);
        pGui.getMenu().getPrint().setEnabled(true);
        pGui.getIconBar().setEnabledPrint(true);
        if (oCtrl.isLocked()) {
            pGui.getIconBar().setEnabledModify(false);
            pGui.getMenu().getModify().setEnabled(false);
        } else {
            pGui.getIconBar().setEnabledModify(write);
            pGui.getMenu().getModify().setEnabled(write);
        }
    }

    private Object getOpenPiece(int type, String pieceId) {
        switch (type) {
            case 0: // '\0'
                msgTab = "DEVIS client";
                pieceIcon = quoteSales;
                return crud.read(pieceId, 22);

            case 1: // '\001'
                msgTab = "COMMANDE client";
                pieceIcon = orderSales;
                return crud.read(pieceId, 22);

            case 2: // '\002'
                msgTab = "BL client";
                pieceIcon = deliverySales;
                return crud.read(pieceId, 22);

            case 3: // '\003'
                msgTab = "FACTURE client";
                pieceIcon = billSales;
                return crud.read(pieceId, 25);

            case 4: // '\004'
                msgTab = "AVOIR client";
                pieceIcon = assetSales;
                return crud.read(pieceId, 25);

            case 5: // '\005'
                msgTab = "COMMANDE fournisseur";
                pieceIcon = orderBuys;
                return crud.read(pieceId, 24);

            case 6: // '\006'
                msgTab = "FACTURE fournisseur";
                pieceIcon = billBuys;
                return crud.read(pieceId, 24);
        }
        return null;
    }

    public void maximize() {
        try {
            pGui.setMaximum(true);
        } catch (PropertyVetoException e) {
            e.printStackTrace();
        }
    }

    public boolean init() {
        if (pGui == null || pGui.isClosed()) {
            return initAll();
        }
        if (pGui.isIcon()) {
            try {
                pGui.setIcon(false);
            } catch (PropertyVetoException e) {
                e.printStackTrace();
            }
        }
        pGui.toFront();
        return false;
    }

    public PiecesGUI getGUI() {
        return pGui;
    }
    private PiecesGUI pGui;
    private Parameter param;
    private JDesktopPane desktop;
    private DeliveryController deliveryCtrl;
    private GroupController groupCtrl;
    private PaymentController paymentCtrl;
    private CustomerPanelController customerCtrl;
    private ItemPanelController itemCtrl;
    private SupplierPanelController supplierCtrl;
    private PieceListController pieceListCtrl;
    private StatisticsWPController statWPCtrl;
    private NewOrderController newOrderCtrl;
    private ReminderWPController reminderWPCtrl;
    private ObjectCRUD crud;
    private String pieceId;
    private String msgTab;
    private Icon quoteSales;
    private Icon orderSales;
    private Icon deliverySales;
    private Icon billSales;
    private Icon assetSales;
    private Icon orderBuys;
    private Icon billBuys;
    private Icon pieceIcon;
    private Icon quoteSalesM;
    private Icon orderSalesM;
    private Icon deliverySalesM;
    private Icon billSalesM;
    private Icon assetSalesM;
    private Icon orderBuysM;
    private Icon billBuysM;
    private Icon pieceIconM;
    private int key;
    private PiecesPrint print;
    private boolean write;
}
